package com.noble.meether.ui.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.camera.ImgSelectUtil;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.utils.FileUtil;
import com.noble.meether.logic.media.MediaConstant;
import com.noble.meether.logic.media.UpLoadResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 意见反馈
 */
public class SuggestAct extends BaseAppCompatActivity implements View.OnClickListener, ImgSelectUtil.OnChooseCompleteListener, RequestComplete {
    private EditText et_suggest;
    private EditText et_contact;
    private CharSequence temp;
    private int maxCount = 120;
    private int selectionStart;
    private int selectionEnd;

    private ImageView iv_current, iv_01, iv_02, iv_03;
    private List<String> imageList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.suggest_layout);
        setBackView(getResources().getString(R.string.title_suggest));
        initView();
    }

    private void initView() {
        initPic();
        et_suggest = this.findViewById(R.id.edt_suggest_value);
        et_contact = this.findViewById(R.id.edt_suggest_qq);
        findViewById(R.id.btn_suggest_send).setOnClickListener(this);

        et_suggest.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                selectionStart = et_suggest.getSelectionStart();
                selectionEnd = et_suggest.getSelectionEnd();
                if (temp.length() > maxCount) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    et_suggest.setText(s);
                    et_suggest.setSelection(tempSelection);//设置光标在最后
                }
            }
        });
    }

    private void initPic() {
        iv_01 = findViewById(R.id.iv_01);
        iv_02 = findViewById(R.id.iv_02);
        iv_03 = findViewById(R.id.iv_03);
        iv_01.setOnClickListener(this);
        iv_02.setOnClickListener(this);
        iv_03.setOnClickListener(this);
    }

    private void submitSuggest() {
        String suggesttext = et_suggest.getText().toString();
        String qq = et_contact.getText().toString();
        if (suggesttext.equals("")) {
            PToast.showShort(getResources().getString(R.string.toast_suggest_isnull));
            et_suggest.setFocusable(true);
            et_suggest.setFocusableInTouchMode(true);
            et_suggest.requestFocus();
            return;
        }
        if (qq.equals("")) {
            PToast.showShort(getResources().getString(R.string.toast_contactr_isnull));
            et_contact.setFocusable(true);
            et_contact.setFocusableInTouchMode(true);
            et_contact.requestFocus();
            return;
        }
        ModuleMgr.getCommonMgr().reqFeedBack(qq, suggesttext, JSON.toJSONString(imageList), this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_01:
                picUpload(iv_01);
                break;
            case R.id.iv_02:
                picUpload(iv_02);
                break;
            case R.id.iv_03:
                picUpload(iv_03);
                break;
            case R.id.btn_suggest_send:
                submitSuggest();
                break;
        }
    }

    private void picUpload(ImageView iv) {
        iv_current = iv;
        ImgSelectUtil.getInstance().pickPhoto(this, this);
    }

    @Override
    public void onUrl(final String url) {
        LoadingDialog.show(this, "文件上传中");
        if (FileUtil.isExist(url)) {
            ModuleMgr.getMediaMgr().sendHttpFile(MediaConstant.UPLOAD_FEED_BACK, url, response -> {
                LoadingDialog.closeLoadingDialog();
                if (!response.isOk()) {
                    PToast.showShort(response.getMsg());
                    return;
                }
                ImageLoader.loadCenterCrop(SuggestAct.this, url, iv_current);
                UpLoadResult result = (UpLoadResult) response.getBaseData();
                imageList.add(result.getUploadUrl());
            });
        } else {
            LoadingDialog.closeLoadingDialog();
            PToast.showShort("图片地址无效");
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqFeedBack) {
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                finish();
            }
        }
    }
}
